const Service = require('egg').Service;

class UserService extends Service {
    async add() {
        const user = { 
            ...this.ctx.request.body,
            username: ['huyiyi','abcde'],
            createDate: new Date().toLocaleString()
        };
        return this.ctx.model.User.create(user);
    }

    // 全部用户
    async all() {
        console.log(this.ctx.query);
        return this.ctx.model.User.find();
    }

    // 获取个人信息
    async getUserInfo() {
        let app = this.app;
        let store = app.getStore();
        let ctx = this.ctx;
        let token = ctx.params.token;
        if (!token) {
            return {msg: '缺少token'};
        } 
        let userInfo = store.get({
            token
        });
        if (userInfo.status === -1) {
            return {msg: userInfo.msg}
        }
        let username = userInfo.user.username;
        let resultSet = await this.app.mongo.find('users',{query:{username}});
        return {
            code: 0,
            msg: 'success',
            data: resultSet[0]
        }
       
    }
}

module.exports = UserService;
